/**
 * Created by 永志 on 2015/5/20.
 */

window.totalPath = {};
window.totalShim = {};
window.totalDependency = [];
window.totalRoutes = [];

var mdeps = [];
//框架的相关定义
mdeps.push('common/dependency/path.js');
mdeps.push('common/dependency/shim.js');
mdeps.push('common/dependency/dependency.js');
mdeps.push('common/dependency/routes.js');

//子系统的相关定义
if ((typeof(globalVar) != "undefined") &&
    (typeof(globalVar.subSystemResourcesPath) != "undefined")){
    for (var key in window.globalVar.subSystemResourcesPath) {
        mdeps.push(window.globalVar.subSystemResourcesPath[key] + '/dependency/path.js');
        mdeps.push(window.globalVar.subSystemResourcesPath[key] + '/dependency/shim.js');
        mdeps.push(window.globalVar.subSystemResourcesPath[key] + '/dependency/dependency.js');
        mdeps.push(window.globalVar.subSystemResourcesPath[key] + '/dependency/routes.js');
    }
};

define(mdeps, function(){
    require.config({
        baseUrl : "./",
        paths : totalPath,
        map: {
            '*': {
                'css': 'common/js/css'
            }
        },
        shim : totalShim
    });

    require(totalDependency,
        function(domReady){

            function router_Ctrl($scope, $routeParams){
//                $scope.routeParams = $routeParams;
//                $scope.maizuoSystemName = maizuoSystemName;
//                $scope.maizuoSystemId = maizuoSystemId;
//                $scope.maizuoSystemName = maizuoSystemName;
//                $scope.maizuoSystemFullName = maizuoSystemFullName;
//                $scope.systemLogoURL = systemLogoURL;
            }

            //定义路由策略
            routeModule.config(function($provide, $routeProvider,$locationProvider,$httpProvider) {
                $locationProvider.html5Mode(false);

                $provide.factory('$routeProvider', function () {
                    return $routeProvider;
                });
                $provide.factory('$locationProvider', function () {
                    return $locationProvider;
                });

                // Initialize get if not there
                if (!$httpProvider.defaults.headers.get) {
                    $httpProvider.defaults.headers.get = {};
                }
                // Enables Request.IsAjaxRequest() in ASP.NET MVC
                //$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
                // Disable IE ajax request caching
                $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
                $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
            }).run(function($routeProvider, $http,$route,$rootScope,ProjectShareData,asyncDataStorage) {
                //定义路由
                angular.forEach(window.totalRoutes, function (route) {
                    $routeProvider.when(route.url, {
                        controller : router_Ctrl,
                        templateUrl: route.templateUrl
                    });
                });
                $routeProvider.otherwise({
                    redirectTo: '/view/404'
                });
                $route.reload();

                //全局监听器
                $rootScope.$on('$locationChangeStart', function(evt, newUrl, currentUrl){
                    if(currentUrl.indexOf("/view/physicsSeatDesigner") > 0 && ProjectShareData.phyEditFlag){
                        var ret = window.confirm("您确定已保存并离开吗？");
                        if(!ret){
                            evt.preventDefault();
                        }
                    }
                    if(newUrl.indexOf("/view/pms/project_list") > 0){
                        ProjectShareData.projectClass = 1;
                    }
                    //如果不是选座页面，副屏显示起始页
                    if($.crosstab && $.crosstab.hasCrosstab() &&  newUrl.indexOf("/view/sale/ppxsxz") < 0
                        && newUrl.indexOf("/view/sale/ppyd") < 0
                        && newUrl.indexOf("/view/sale/ppxsxz") < 0
                        && newUrl.indexOf("/view/sale/fpgl") < 0) {
                        $.crosstab.sendMessage({
                            msgType : MSG_TYPES.FLOOR_INIT_INFO,
                            msg : 1
                        });
                    }
                    if(newUrl.indexOf("/view/pms/coupon_project_list") > 0){
                        ProjectShareData.projectClass = 2;
                    }
                    if(newUrl.indexOf("/view/pms/coupon_project_list") > 0){
                        ProjectShareData.projectClass = 2;
                    }
                    
                    //新地址为下单
                    if(newUrl.indexOf("/view/sale/xbpj") != -1 && (
                    		currentUrl.indexOf("/view/sale/ppxsxz") == -1 //老地址即不是选座也不是正式订单购物车
                    				&&  currentUrl.indexOf("/view/sale/shopCart_formal") == -1 )){
                    	console.log("清除购物车了");
                    	asyncDataStorage.saveItem("user_buy_goods",[],1);
                    }else if(newUrl.indexOf("/view/sale/xbys") != -1 && (
	                		currentUrl.indexOf("/view/sale/ppyd") == -1 //老地址即不是选座也不是正式订单购物车
            						&&  currentUrl.indexOf("/view/sale/shopCart_advance") == -1 )){
	                	console.log("清除购物车了");
	                	asyncDataStorage.saveItem("user_buy_goods",[],2);
	                }
//                    else if(newUrl.indexOf("/view/sale/xbys") != -1 && (
//                        currentUrl.indexOf("/view/sale/ppyd") == -1 //老地址即不是选座也不是正式订单购物车
//                        &&  currentUrl.indexOf("/view/sale/advance_reChoice") == -1 )){
//                        console.log("清除购物车了");
//                        dataStorage.saveItem("user_buy_goods",[]);
//                    }else if(newUrl.indexOf("/view/sale/xbys") != -1 && (
//                        currentUrl.indexOf("/view/sale/ppyd") == -1 //老地址即不是选座也不是正式订单购物车
//                        &&  currentUrl.indexOf("/view/sale/advance_reChoice_order") == -1 )){
//                        console.log("清除购物车了");
//                        dataStorage.saveItem("user_buy_goods",[]);
//                    }else if(newUrl.indexOf("/view/sale/xbys") != -1 && (
//                        currentUrl.indexOf("/view/sale/ppyd") == -1 //老地址即不是选座也不是正式订单购物车
//                        &&  (currentUrl.indexOf("/view/sale/advance_split_order") == -1
//                        || currentUrl.indexOf("/view/sale/advance_split") == -1))){
//                        console.log("清除购物车了");
//                        dataStorage.saveItem("user_buy_goods",[]);
//                    }
                    
                });
            });

            domReady(function() {
                $('html').addClass('ng-app:msd');
                angular.bootstrap(document, ['msd']);
                $(".sysname").text(window.globalVar.sysname);

                //禁止敲击退格键时的页面跳转
                document.onkeydown = function (event) {
                    var event = event || window.event;
                    /*屏蔽退格删除键*/
                    if ((event.keyCode==8) )  {
                        if (event.srcElement.tagName.toUpperCase()!="INPUT"
                            && event.srcElement.tagName.toUpperCase()!="TEXTAREA"
                            && event.srcElement.tagName.toUpperCase()!="TEXT") {
                            event.keyCode=0;
                            event.returnValue=false;
                        }
                    }else if(event.keyCode == 13){ //
                    	if (event.srcElement.tagName.toUpperCase()=="INPUT"
                            || event.srcElement.tagName.toUpperCase()=="TEXTAREA"
                            || event.srcElement.tagName.toUpperCase()=="TEXT") {
                    		event.returnValue = true;
                        }else{
                        	event.returnValue = false;
                        }
                    }
                }
            });


            //弹窗滚动
            document.addEventListener('mousewheel', function(ev) {
                if($("body").hasClass('modal-open')) {
                    if($(".modal.in").css("display")=="block") {
                        var $box = $(this).find(".modal-body");
                        var ev = ev||event;
                        var bool = false;
                        //IE、chrome 向上：120，向下：-120
                        if(ev.wheelDelta){
                            bool= ev.wheelDelta > 0? true : false;
                        }
                        //firefox 向上：-3，向下：3
                        else{
                            bool= ev.detail < 0? true : false;
                        }

                        if(bool) {

                            $box.animate({ scrollTop:$box.scrollTop() - 100} , 30);

                        } else{
                            //必须设置一个大值, 由于在编辑弹窗获取scrollTop一直等于0
                            $box.animate({ scrollTop: 1000000 } , 0);

                        }
                    } else {
                        return false;
                    }

                }
            });



        });
});
